package com.eightbears.beboy.base.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightbears.beboy.R;


/**
 * @author 消息对话框
 */
public class MessageDialog extends Dialog {

    public MessageDialog(Context context) {
        super(context);
    }

    public MessageDialog(Context context, int theme) {
        super(context, theme);
    }

    public static class Builder {

        private Context context;
        private String title;
        private Bitmap bitmap;
        private String tv_price;
        private String tv_code;
        private String message;
        private String forget;
        private String reset;
        private String resetPw;

        private String order_sure;
        private String order_cancel;
        private View contentView;

        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTv_code(String tv_code) {
            this.tv_code = tv_code;
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTv_price(String tv_price) {
            this.tv_price = tv_price;
            return this;
        }


        /**
         * Set the Dialog title from String
         *
         * @param message
         * @return
         */

        public Builder setContent(String message) {
            this.message = message;
            return this;
        }

        public Builder setforget(String forget) {
            this.forget = forget;
            return this;
        }

        public Builder setReset(String reset) {
            this.reset = reset;
            return this;
        }

        public Builder setPositiveButton(int order_sure,
                                         OnClickListener listener) {
            this.order_sure = (String) context
                    .getText(order_sure);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setResetButton(String reset,
                                      OnClickListener listener) {
            this.reset = reset;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int order_cancel,
                                         OnClickListener listener) {
            this.order_cancel = (String) context
                    .getText(order_cancel);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setForgetButton(String forget,
                                       OnClickListener listener) {
            this.forget = forget;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setForgetPwButton(String resetPw,
                                         OnClickListener listener) {
            this.resetPw = resetPw;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public MessageDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final MessageDialog dialog = new MessageDialog(context, R.style.Dialog);
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            View layout = inflater.inflate(R.layout.dialog_message, null);
            dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

            if (message != null && message.length() > 0) {
                ((TextView) layout.findViewById(R.id.tv_content)).setText(message);
            } else {
                layout.findViewById(R.id.tv_content).setVisibility(View.GONE);
            }

            if (reset != null && reset.length() > 0) {
                ((TextView) layout.findViewById(R.id.tv_reset)).setText(reset);
            } else {
                layout.findViewById(R.id.tv_reset).setVisibility(View.GONE);
            }

            if (forget != null && forget.length() > 0) {
                ((TextView) layout.findViewById(R.id.tv_forget)).setText(forget);
            } else {
                layout.findViewById(R.id.tv_forget).setVisibility(View.GONE);
            }

//          重试
            if (reset != null) {
                ((TextView) layout.findViewById(R.id.tv_reset)).setText(reset);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.tv_reset))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                (layout.findViewById(R.id.tv_reset)).setVisibility(View.GONE);
            }

//            //忘记密码
            if (forget != null) {
                ((TextView) layout.findViewById(R.id.tv_forget)).setText(forget);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.tv_forget))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                (layout.findViewById(R.id.tv_forget)).setVisibility(View.GONE);
            }

            //重置密码
            if (resetPw != null) {
                ((TextView) layout.findViewById(R.id.tv_reset_pw)).setText(resetPw);
                (layout.findViewById(R.id.tv_reset_pw)).setVisibility(View.VISIBLE);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.tv_reset_pw))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            }
            dialog.setContentView(layout);
            return dialog;
        }

    }
}
